package medOffice;

import java.sql.SQLException;
import java.sql.Time;
import java.util.GregorianCalendar;

import medOffice.Enuns.Sexo;
import medOffice.dao.tests.ConsultaDAOTest;
import medOffice.dao.tests.MedicoDAOTest;
import medOffice.dao.tests.PacienteDAOTest;
import medOffice.dao.tests.PlanoDAOTest;
import medOffice.dao.tests.RepresentanteDAOTest;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Testes da clinica.
 * @author Amilton Acioli 
 *
 */
public class ClinicaTest {

	Clinica c1, c2;
	Paciente p1, p2, p3;
	PlanoDeSaude ps1, ps2;
	Representante r1, r2;
	
	class MinhaAgenda extends Agenda{

		public MinhaAgenda(Medico medico) throws Exception {
			super(medico);
			super.consultaDAO = new ConsultaDAOTest();
		}
		
	}
	
	class MinhaClinica extends Clinica{
		public MinhaClinica(String nome) throws Exception {
			super(nome);
			daoRepresentante = new RepresentanteDAOTest();
			daoPaciente = new PacienteDAOTest();
			daoMedico = new MedicoDAOTest();
			daoPlano = new PlanoDAOTest();
		}
	}

	@Before
	public void setUp() throws Exception {
		GerarBDTest.main(null);
		c1 = new MinhaClinica("Unimed");
		
		c2 = new MinhaClinica("Memorial Sao Francisco");
		
		ps1 = new PlanoDeSaude("Unimed", "(83) 3333-1122", 50.0, 0.0, 152233);
		c1.addPlanoDeSaude(ps1);
		
		ps1 = new PlanoDeSaude("Unmao", "(83) 3333-1122", 50.0, 0.0, 152244);
		c1.addPlanoDeSaude(ps1);
		
		
		Medico med1 = new Medico("Jenesio", "000000000-12");
		med1.adicionaHorarioDisponivel(3, new Time[] {
				Time.valueOf("12:00:00"), Time.valueOf("16:00:00") });
		med1.adicionaPlanoDeSaude(new PlanoDeSaude("Unimed", "(83) 3222-2222",
				200, 100, 1));
		
		med1.setTempoConsulta("00:30:00");
		med1.setTempoRetorno("00:20:00");
		
		med1.setDataDeNascimento(new GregorianCalendar(1990,1,10));
		med1.setSexo(Sexo.M);
		
		try{
			c1.addMedico(med1);
		}catch(Exception ex){
			
		}
		
		c1.setAgenda(new MinhaAgenda(med1));
		
		p1 = new Paciente("Ana Clara", "123123123-12", "rua joao pessoa", "(83) 8721-9963", new GregorianCalendar(1987,12,27),"aninhaa136@gmail.com", Sexo.F ,ps1);
		p2 = new Paciente("Tiago Leite","064857574-81", "rua antonio joaquim", "(83) 8865-3972", new GregorianCalendar(1987,12,27),"chalanger@gmail.com", Sexo.M, ps1);
		p3 = new Paciente("Amilton Aciole", "000000000-00", "rua sei la onde", "(83) 8882-9632", new GregorianCalendar(1987,12,27), "amiltonacioli@gmail.com",Sexo.M, ps1);
		

	}

	@Test(expected=Exception.class)
	public void testClinicaNomeNull() throws Exception {
		Clinica c = new MinhaClinica(null);
	}


	@Test(expected = Exception.class)
	public void testNomeClinicaVazio() throws Exception {
		Clinica c = new MinhaClinica("");
	}

	// Testes de adicao e remocao de Pacientes na clinica
	@Test
	public void testAddPaciente() throws Exception {
		
		Assert.assertEquals("lista planos erradas", 2 ,c1.getListaDePlanos().size());
		Assert.assertTrue("Deveria ter adicionado", c1.addPaciente(p1));
		Assert.assertFalse("Nao deveria ter adicionado",c1.addPaciente(p1));
		Assert.assertTrue("Deveria ter adicionado", c1.addPaciente(p2));
		Assert.assertEquals("Erro na adicao dos Pacientes", c1.getListaPacientes().size(), 2);
	}
	
	@Test 
	public void testRemovePaciente() throws Exception {
		
		Assert.assertTrue("Deveria ter adicionado", c1.addPaciente(p1));
		Assert.assertTrue(c1.removePaciente(p1.getNome(), p1.getCPF()));
		Assert.assertEquals("Erro na remocao do Paciente", 0, c1.getListaPacientes().size());
		Assert.assertFalse(c1.removePaciente(p2.getNome(), p2.getCPF()));		
	}
	
	@Test
	public void testAddPlano() throws Exception {
		ps1 = new PlanoDeSaude("med", "(83) 3333-1122", 50.0, 0.0, 111133);
		Assert.assertTrue("Deveria ter adicionado",c1.addPlanoDeSaude(ps1));
		ps1 = new PlanoDeSaude("Unmao", "(83) 3333-1122", 50.0, 0.0, 1526056);
		Assert.assertTrue("Deveria ter adicionado",c1.addPlanoDeSaude(ps1));
		
		Assert.assertEquals("erro no add", 4, c1.getListaDePlanos().size());
		
		ps1 = new PlanoDeSaude("med", "(83) 3333-1122", 50.0, 0.0, 111133);
		Assert.assertFalse("Nao Deveria ter adicionado",c1.addPlanoDeSaude(ps1));
		ps1 = new PlanoDeSaude("Unmao", "(83) 3333-1122", 50.0, 0.0, 1526056);
		Assert.assertFalse("Nao Deveria ter adicionado",c1.addPlanoDeSaude(ps1));
		
		Assert.assertEquals("erro no add", 4, c1.getListaDePlanos().size());
		
	}
	
	@Test
	public void testRemovePlano() throws Exception {
		testAddPlano();
		int numPlanos = c1.getListaDePlanos().size();
		for(PlanoDeSaude plano: c1.getListaDePlanos()){
			Assert.assertEquals("erro ao remover", numPlanos, c1.getListaDePlanos().size());
			Assert.assertTrue("Deveria ter removido",c1.removePlanoDeSaude(plano));
			numPlanos--;
		}
		
		Assert.assertEquals("erro ao remover", numPlanos, c1.getListaDePlanos().size());
		
		ps1 = new PlanoDeSaude("med", "(83) 3333-1122", 50.0, 0.0, 111133);
		Assert.assertFalse("nao Deveria ter removido",c1.removePlanoDeSaude(ps1));
	}
	
	@Test
	public void testAddRepresentante() throws Exception {
		Representante rep1 = new Representante("joao", "jmed");
		rep1.setId(1234);
		rep1.setTelefone("(83) 3333-1122");
		rep1.setTempoLimite(30);
		rep1.setUltimaDataDeVisita(new GregorianCalendar(2009,11,02));
		
		Assert.assertTrue("Deveria ter adicionado", c1.addRepresentante(rep1));
		
		rep1.setId(50);
		Assert.assertTrue("Deveria ter adicionado", c1.addRepresentante(rep1));
		
		Assert.assertFalse("Nao Deveria ter adicionado", c1.addRepresentante(rep1));
		
		rep1.setId(100);
		Assert.assertTrue("Deveria ter adicionado", c1.addRepresentante(rep1));
		
		Assert.assertEquals("Adicionou errado", c1.getListaRepresentantes().size(), 3);
		
		rep1.setId(150);
		Assert.assertTrue("Deveria ter adicionado", c1.addRepresentante(rep1));
		
		Assert.assertEquals("Adicionou errado", c1.getListaRepresentantes().size(), 4);
		
		Assert.assertFalse("Deveria ter adicionado", c1.addRepresentante(rep1));
		
		Assert.assertEquals("Adicionou errado", c1.getListaRepresentantes().size(), 4);
		
	}
	
	@Test
	public void testRemoveRepresentante() throws Exception {
		testAddRepresentante();
		int numRep = c1.getListaRepresentantes().size();
		for(Representante rep: c1.getListaRepresentantes()){
			Assert.assertEquals("erro ao remover", numRep, c1.getListaRepresentantes().size());
			Assert.assertTrue("Deveria ter removido",c1.removeRepresentante(rep.getNome(),rep.getId()));
			numRep--;
		}
		
		Assert.assertEquals("erro ao remover", numRep, c1.getListaRepresentantes().size());
		
		Representante rep1 = new Representante("joao", "jmed");
		rep1.setId(1234);
		rep1.setTelefone("(83) 3333-1122");
		rep1.setTempoLimite(30);
		rep1.setUltimaDataDeVisita(new GregorianCalendar(2009,11,02));
		Assert.assertFalse("nao Deveria ter removido",c1.removeRepresentante(rep1.getNome(),rep1.getId()));
		
		Assert.assertEquals("erro ao remover", 0, c1.getListaRepresentantes().size());
	}
	
	@Test
	public void testEquals() throws Exception {
		Assert.assertFalse("Deviam ser diferentes", c1.equals(c2));
		c2.setNomeDaClinica("Unimed");
		Assert.assertTrue("Deviam ser iguais", c1.equals(c2));		
	}
	
	
	public void limpaTabela(Clinica c) throws SQLException {
		
		for (int i = 0; i < c.getListaPacientes().size(); i++) {
			c.removePaciente(c.getListaPacientes().get(i).getNome(), c.getListaPacientes().get(i).getCPF() );			
		}
	}
	

}

